package club.creategeek.ctrl.test;

import com.tongze.common.oss.service.OssService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 提现结算记录表
 *
 * @author xiaoyu
 * @site https://www.jeequan.com
 * @date 2022/3/21 10:00
 */
@RestController
@RequestMapping("/test")
public class CashoutRecordController {

    @Resource
    private OssService ossService;

    @GetMapping()
    public String detail() {
        String aa =  ossService.uploadFile(null);
        System.out.println(aa);
        return "hello";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String readBankCard(MultipartFile imgFile) {
        return ossService.uploadFile(imgFile, "test");
    }


    @GetMapping(value = "/download")
    @ResponseBody
    public void asset(String fileName, HttpServletResponse response) {
        response.reset();
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        try {
            InputStream inputStream = ossService.downloadFile(fileName, "test");
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            inputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


}
